package com.ruoyi.purchase.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.purchase.domain.AllocationPlan;
import com.ruoyi.purchase.service.IAllocationPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Objects;

/**
 * AllocationPlanController
 * 
 * @author kevinness
 * @date 2025-01-17
 */
@RestController
@RequestMapping("/AllocationPlan")
public class AllocationPlanController extends BaseController
{
    @Autowired
    private IAllocationPlanService allocationPlanService;

    /**
     * 查询AllocationPlan列表
     */
    @GetMapping("/list")
    public TableDataInfo list(AllocationPlan allocationPlan)
    {
        startPage();
        List<AllocationPlan> list = allocationPlanService.selectAllocationPlanList(allocationPlan);
        return getDataTable(list);
    }

    /**
     * 导出AllocationPlan列表
     */
    @Log(title = "AllocationPlan", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AllocationPlan allocationPlan)
    {
        List<AllocationPlan> list = allocationPlanService.selectAllocationPlanList(allocationPlan);
        ExcelUtil<AllocationPlan> util = new ExcelUtil<AllocationPlan>(AllocationPlan.class);
        util.exportExcel(response, list, "AllocationPlan数据");
    }

    /**
     * 获取AllocationPlan详细信息
     */
    @GetMapping(value = "/{allocationNo}")
    public AjaxResult getInfo(@PathVariable("allocationNo") String allocationNo)
    {
        return success(allocationPlanService.selectAllocationPlanByAllocationNo(allocationNo));
    }

    /**
     * 新增AllocationPlan
     */
    @Log(title = "AllocationPlan", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AllocationPlan allocationPlan)
    {
        //添加修改时间、修改人信息
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String formattedNow = now.format(formatter);
        String username = SecurityUtils.getUsername();
        allocationPlan.setModifiedDate(formattedNow);
        allocationPlan.setModifiedBy(username);
        allocationPlan.setCreatedTime(formattedNow);

        return toAjax(allocationPlanService.insertAllocationPlan(allocationPlan));
    }

    /**
     * 修改AllocationPlan
     */
    @Log(title = "AllocationPlan", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AllocationPlan allocationPlan)
    {
        //删除原有计划
        String[] allocationNos = new String[1];
        allocationNos[0] = allocationPlan.getAllocationNo();
        allocationPlanService.deleteAllocationPlanByAllocationNos(allocationNos);

        //添加修改时间、修改人信息
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String formattedNow = now.format(formatter);
        String username = SecurityUtils.getUsername();
        allocationPlan.setModifiedDate(formattedNow);
        allocationPlan.setModifiedBy(username);

        //插入新信息
        return toAjax(allocationPlanService.insertAllocationPlan(allocationPlan));
    }

    /**
     * 删除AllocationPlan
     */
    @Log(title = "AllocationPlan", businessType = BusinessType.DELETE)
	@DeleteMapping("/{allocationNos}")
    public AjaxResult remove(@PathVariable String[] allocationNos)
    {
        for (String allocationNo : allocationNos){
            AllocationPlan allocationPlan = allocationPlanService.selectAllocationPlanByAllocationNo(allocationNo);
            if(allocationPlan != null && Objects.equals(allocationPlan.getState(), "已分配运输订单")){
                return error("已分配调拨订单的计划无法删除");
            }
        }
        return toAjax(allocationPlanService.deleteAllocationPlanByAllocationNos(allocationNos));
    }

    /**
     * 通过订单编号获取调拨计划
     */
    @PostMapping("/getAllocationByOrderNo")
    public AjaxResult getAllocationByOrderNo(@RequestBody String orderNo) {
        return success(allocationPlanService.selectAllocationPlanByOrderNo(orderNo));
    }
}
